package ft.app.gtig.portal.controllers.api;

import com.haulmont.cuba.client.ClientConfig;
import com.haulmont.cuba.core.app.DataService;
import com.haulmont.cuba.core.entity.FileDescriptor;
import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.Configuration;
import com.haulmont.cuba.core.global.FileTypesHelper;
import com.haulmont.cuba.core.global.LoadContext;
import com.haulmont.cuba.core.sys.AppContext;
import com.haulmont.cuba.core.sys.SecurityContext;
import com.haulmont.cuba.core.sys.remoting.ClusterInvocationSupport;
import com.haulmont.cuba.core.sys.remoting.LocalFileExchangeService;
import com.haulmont.cuba.security.app.UserSessionService;
import com.haulmont.cuba.security.global.NoUserSessionException;
import com.haulmont.cuba.security.global.UserSession;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.inject.Inject;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.UUID;
import java.util.regex.Pattern;

@Controller("appFileDownload")
@RequestMapping("/app")
@CrossOrigin(origins = "*")
public class FileDownloadController {

    private static Logger log = LoggerFactory.getLogger(FileDownloadController.class);

    @Inject
    protected DataService dataService;

    @Inject
    protected UserSessionService userSessionService;

    @Resource(name = ClusterInvocationSupport.NAME)
    protected ClusterInvocationSupport clusterInvocationSupport;

    protected String fileDownloadContext;

    protected Pattern uuidPattern = Pattern.compile("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})");

    @Inject
    public void setConfiguration(Configuration configuration) {
        fileDownloadContext = configuration.getConfig(ClientConfig.class).getFileDownloadContext();
    }

    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {

        try {
            String srcId = request.getParameter("f");
            if (srcId.length() == 32) {
                srcId = uuidPattern.matcher(srcId).replaceAll("$1-$2-$3-$4-$5");
            }

            UUID fileId = UUID.fromString(srcId);

            FileDescriptor fd = dataService.load(LoadContext.create(FileDescriptor.class).setId(fileId));
            if (fd == null) {
                log.warn("Unable to find file with id " + fileId);
                error(response);
                return;
            }

            response.setHeader("Cache-Control", "no-cache");
            response.setHeader("Pragma", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setHeader("Content-Type", getContentType(fd));
            response.setHeader("Pragma", "no-cache");

//            String fileName = URLEncoder.encode(fd.getName(), StandardCharsets.UTF_8.name());
//            boolean attach = Boolean.valueOf(request.getParameter("a"));
//            response.setHeader("Content-Disposition", (attach ? "attachment" : "inline")
//                    + "; filename=" + fileName);

            writeResponse(response, fd);

        } catch (Exception ex) {
            log.error("download error.", ex);
            error(response);
        }
    }

    private void writeResponse(HttpServletResponse response, FileDescriptor fd) throws IOException {

        ServletOutputStream os = response.getOutputStream();

        try {
            InputStream is = AppBeans.get(LocalFileExchangeService.NAME, LocalFileExchangeService.class).downloadFile(fd);
            IOUtils.copy(is, os);
            os.flush();
        } catch (IOException ex) {
            log.error("Unable to download file", ex);
            error(response);
        } finally {
            IOUtils.closeQuietly(os);
        }
    }

    private void writeResponse(HttpServletResponse response, UserSession userSession, FileDescriptor fd)
            throws IOException {
        InputStream is = null;
        ServletOutputStream os = response.getOutputStream();
        try {
            for (Iterator<String> iterator = clusterInvocationSupport.getUrlList().iterator(); iterator.hasNext(); ) {
                String url = iterator.next() + fileDownloadContext +
                        "?s=" + userSession.getId() +
                        "&f=" + fd.getId().toString();

                HttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);

                try {
                    HttpResponse httpResponse = httpClient.execute(httpGet);
                    int httpStatus = httpResponse.getStatusLine().getStatusCode();
                    if (httpStatus == HttpStatus.SC_OK) {
                        HttpEntity httpEntity = httpResponse.getEntity();
                        if (httpEntity != null) {
                            is = httpEntity.getContent();
                            IOUtils.copy(is, os);
                            os.flush();
                            break;
                        } else {
                            log.debug("Unable to download file from " + url + "\nHttpEntity is null");
                            if (iterator.hasNext())
                                log.debug("Trying next URL");
                            else
                                error(response);
                        }
                    } else {
                        log.debug("Unable to download file from " + url + "\n" + httpResponse.getStatusLine());
                        if (iterator.hasNext())
                            log.debug("Trying next URL");
                        else
                            error(response);
                    }
                } catch (IOException ex) {
                    log.debug("Unable to download file from " + url + "\n" + ex);
                    if (iterator.hasNext())
                        log.debug("Trying next URL");
                    else
                        error(response);
                } finally {
                    IOUtils.closeQuietly(is);
                    httpClient.getConnectionManager().shutdown();
                }
            }
        } finally {
            IOUtils.closeQuietly(os);
        }
    }

    protected UserSession getSession(HttpServletRequest request, HttpServletResponse response) {
        UUID sessionId;
        try {
            sessionId = UUID.fromString(request.getParameter("s"));
        } catch (Exception e) {
            return null;
        }

        AppContext.setSecurityContext(new SecurityContext(sessionId));
        try {
            UserSession userSession = userSessionService.getUserSession(sessionId);
            return userSession;
        } catch (NoUserSessionException e) {
            return null;
        } finally {
            AppContext.setSecurityContext(null);
        }
    }

    protected String getContentType(FileDescriptor fd) {
        if (StringUtils.isEmpty(fd.getExtension())) {
            return FileTypesHelper.DEFAULT_MIME_TYPE;
        }

        return FileTypesHelper.getMIMEType("." + fd.getExtension().toLowerCase());
    }

    private void error(HttpServletResponse response) throws IOException {
        if (!response.isCommitted())
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
}